package com.shujia.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo6Window {

  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val ds: DataStream[String] = env.socketTextStream("master",8888)

    val kvDS: DataStream[(String, Int)] = ds.flatMap(_.split(","))
      .map((_,1))

    //kvDS.print()

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    //5秒一个窗口
    val winDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS.timeWindow(Time.seconds(5))

    //开窗之后求和
    //winDS.reduce((kv1,kv2) => (kv1._1,kv1._2 + kv2._2)).print()
    winDS.sum(1).print()

    env.execute()

  }

}
